﻿#region using
using MpSoft.IO;
using System;
using System.IO;
#endregion using

namespace PdbRestorer.Engine
{
	public static class SimplePePdbReader
	{
		public static bool GetSignatureAndAge(string assemblyFilename,out Guid signature,out int age)
		{
			using (Stream stream=File.OpenRead(assemblyFilename))
				return GetSignatureAndAge(stream,out signature,out age);
		}

		public static bool GetSignatureAndAge(Stream assembly,out Guid signature,out int age)
		{
			byte[] debugDirectoryCvSignature=BitConverter.GetBytes((int)0x53445352);

			if (assembly.Find(debugDirectoryCvSignature))
			{
				byte[] buffer=new byte[16];
				if (assembly.Read(buffer,0,16)!=16)
					throw new EndOfStreamException();
				signature=new Guid(buffer);
				if (assembly.Read(buffer,0,4)!=4)
					throw new EndOfStreamException();
				age=BitConverter.ToInt32(buffer,0);
				return true;
			}
			signature=default(Guid);
			age=default(int);
			return false;
		}
	}
}
